package org.vectrics.recruiter.struts.tag;

import org.apache.log4j.Logger;
import org.vectrics.common.html.Table;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.user.Person;

/**
 * @author mmoore
 */
public class UserBufferForJobCandidateTag extends UserBufferTag {

	private static final long serialVersionUID = 7520807629388213012L;
	
	private transient final Logger log = Logger.getLogger(this.getClass());
    private String jobCandidateId = null;
    
    private JobCandidate getJobCandidate() {
        JobCandidate jobCandidate = RecruitServiceLocator.getJobProvider().findJobCandidateById(new Long(jobCandidateId));
        return(jobCandidate);
    }
    
    protected void createJobBufferContent(Table contentTable, Job jobForBuffer) {
        
    }
    
    protected void createPersonBufferContent(Table contentTable, Person personForBuffer) {
    	if (log.isDebugEnabled())
    		log.debug("createPersonBufferContent() - person = " + personForBuffer);
	
        if (jobCandidateId != null) {
        	CandidateInfo candidate = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(personForBuffer);
        	if (candidate != null) {
	        	if (getJobCandidate().getJob().findJobCandidate(candidate) == null) {
			    	String href = this.getContextPath() + "/admin/jobs/candidate/add.do?jobId=" + getJobCandidate().getJob().getId() 
			    	        + "&userId=" + personForBuffer.getId();
			    	this.makeLinkRow(contentTable, this.getPhrase("phrase.make.candidate.for.job"), 
			    			href);
	        	} else {
	        		log.debug("Person is already candidate for job: " + personForBuffer);
	        	}
        	} else {
		    	String href = this.getContextPath() + "/admin/jobs/candidate/add.do?jobId=" + getJobCandidate().getJob().getId() 
    	        	+ "&userId=" + personForBuffer.getId();
		    	this.makeLinkRow(contentTable, this.getPhrase("phrase.make.candidate.for.job"), 
		    			href);
        	}
        	
        	
        	if ((getJobCandidate().getSource() == null) 
        			|| (getJobCandidate().getSource().getId().equals(personForBuffer.getId()) == false)) {
        		log.debug("Buffered person is not source for job candidate, give option to make them the source");
		    	
        		String href = this.getContextPath() + "/admin/jobs/candidate/source/add.do?jobCandidateId=" + getJobCandidate().getId() 
    	        	+ "&personId=" + personForBuffer.getId();
		    	this.makeLinkRow(contentTable, this.getPhrase("phrase.make.source.for.candidate"), 
		    			href);
        	} else {
        		log.debug("Person is already source for job candidate, \r\n - person=" + personForBuffer.toString()
        				+ "\r\n - Current source for job=" + getJobCandidate().getSource());
        	}
        } else {
            log.debug("Job Candidate ID not specified, can't make candidate link");
        }
	        
    }
    
    /**
     * @return Returns the jobId.
     */
    public String getJobCandidateId() {
        return jobCandidateId;
    }
    /**
     * @param jobId The jobId to set.
     */
    public void setJobCandidateId(String jobCandidateId) {
        this.jobCandidateId = jobCandidateId;
    }
}
